<?php

namespace App\Models\Api;

use App\Models\Common\CollOption;

class CompanyBasic extends ApiModel
{
    //表名
    protected $table = 'company_basic';

    const CREATED_AT = 'addtime';
    const UPDATED_AT = 'updatetime';

    protected $guarded = [];

    /**
     * The attributes that should be hidden for arrays.
     * 隐藏字段
     * @var array
     */
    protected $hidden = ['addtime', 'updatetime','region_area'];

    protected $casts = [
        'id' => 'string',
    ];

    public function company_users()
    {
        return $this->belongsTo(CompanyUser::class, 'uid');
    }

    public function company_job()
    {
        return $this->hasMany(CompanyJob::class, 'com_id')->orderBy('updatetime', 'desc');
    }

    public function part_time_job()
    {
        return $this->hasMany(PartTimeJob::class, 'com_id')->orderBy('updated_at', 'desc');
    }

    /**
     * 获取行业名
     *
     * @return string
     */
    public function getIndustryNameAttribute()
    {
        $id = $this->attributes['industry'];
        if ($id == 0) {
            return '';
        }
        return CollOption::getOptionName($id, 'industry');
    }

    /**
     * 获取类型名
     *
     * @return string
     */
    public function getComkindNameAttribute()
    {
        $id = $this->attributes['comkind'];
        if ($id == 0) {
            return '';
        }
        return CollOption::getOptionName($id, 'comkind');
    }

    /**
     * 获取规模名
     *
     * @return string
     */
    public function getEmployeeNumNameAttribute()
    {
        $id = $this->attributes['employee_num'];
        if ($id == 0) {
            return '';
        }
        return CollOption::getOptionName($id, 'employee_num');
    }

    /**
     * 获取地区名
     *
     * @return string
     */
    public function getRegionNameAttribute()
    {
        return $this->region_area ? $this->region_area->name : '';
    }

    public function region_area()
    {
        return $this->hasOne(CollArea::class, 'cid', 'region')->select('cid', 'name');
    }

    /** logo
     * @param $value
     * @return string
     */
    public function getLogoAttribute($value)
    {
        return $value ? 'companylogo/' . $value : '';
    }

    public function getEstablishedNameAttribute()
    {
        return $this->established ? substr($this->attributes['established'], 0, 7) : '';
    }
}
